<!--
 * @Description: 充值/扣除增值服务费操作弹窗
 * @Author: liyujie
 * @Date: 2021/08/31
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/08/31
-->
<template>
    <section class="wrapper">
        <el-dialog
            :title="type === 1 ? '增值服务费充值' : '增值服务费扣除'"
            :visible.sync="dialogVisible"
            width="680px"
            :close-on-click-modal="false"
            @close="beforeClose"
        >
            <div class="dialog-content">
                <el-form
                    :model="ruleForm"
                    :rules="rules"
                    ref="ruleForm"
                    label-width="94px"
                >
                    <div class="form-title">
                        商家信息
                    </div>
                    <div class="shop-info">
                        <div class="shop-info-item">
                            <div class="shop-info-item__label">公司名称：</div>
                            <div class="shop-info-item__value">{{ shopInfo.company_name }}</div>
                        </div>
                        <div class="shop-info-item">
                            <div class="shop-info-item__label">店铺名称：</div>
                            <div class="shop-info-item__value">{{ shopInfo.shop_title }}</div>
                        </div>
                        <div class="shop-info-item">
                            <div class="shop-info-item__label">当前增值服务费：</div>
                            <div class="shop-info-item__value">{{ shopInfo.vas_money }}元</div>
                        </div>
                    </div>
                    <div
                        class="form-title"
                        style="margin-top: 20px"
                    >
                        {{ type === 1 ? '充值信息' : '扣除信息' }}
                    </div>
                    <!--充值-->
                    <template v-if="type === 1">
                        <el-form-item
                            label="充值金额："
                            prop="money"
                        >
                            <el-input
                                v-model="ruleForm.money"
                                size="medium"
                                placeholder="请输入充值金额"
                            >
                                <span
                                    slot="suffix"
                                    style="margin-right: 6px"
                                >元</span>
                            </el-input>
                        </el-form-item>
                        <el-form-item
                            label="充值备注："
                            prop="remark"
                        >
                            <el-input
                                v-model="ruleForm.remark"
                                size="medium"
                                placeholder="请填写增值服务费充值的备注"
                            />
                        </el-form-item>
                        <el-form-item
                            label="充值凭证："
                            prop="certificate_images"
                        >
                            <image-upload-multiple
                                v-model="ruleForm.certificate_images"
                                limit-type="MB"
                                :limit="2"
                                :max="9"
                                text="上传凭证图"
                            />
                        </el-form-item>
                    </template>
                    <!--扣除-->
                    <template v-else>
                        <el-form-item
                            label="扣除金额："
                            prop="money"
                        >
                            <el-input
                                v-model="ruleForm.money"
                                size="medium"
                                placeholder="请输入扣除金额"
                            >
                                <span
                                    slot="suffix"
                                    style="margin-right: 6px"
                                >元</span>
                            </el-input>
                            <div class="form-tips">*扣除的金额不可超过当前的增值服务费</div>
                        </el-form-item>
                        <el-form-item
                            label="扣除原因："
                            prop="reason"
                        >
                            <el-input
                                v-model="ruleForm.reason"
                                type="textarea"
                                placeholder="请填写增值服务费扣除的原因"
                                :autosize="{ minRows: 3}"
                            />
                        </el-form-item>
                        <el-form-item
                            label="扣除凭证："
                            prop="certificate_images"
                        >
                            <image-upload-multiple
                                v-model="ruleForm.certificate_images"
                                limit-type="MB"
                                :limit="2"
                                :max="9"
                                text="上传凭证图"
                            />
                        </el-form-item>
                    </template>

                </el-form>
            </div>
            <div class="dialog-footer">
                <el-button
                    size="medium"
                    style="width: 126px; margin-right: 51px"
                    @click="dialogVisible = false"
                >
                    取消
                </el-button>
                <el-button
                    type="primary"
                    size="medium"
                    style="width: 126px"
                    :loading="loading"
                    @click="handleSubmit"
                >
                    {{ type === 1 ? '立即充值' : '确定' }}
                </el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
import Vue from "vue";
import ImageUploadMultiple from "@/components/common/image-upload-multiple";
import {deepClone} from "@/assets/js/utils";
const numberReg = /(?:^[1-9]([0-9]+)?(?:\.[0-9]{1,2})?$)|(?:^(?:0)$)|(?:^[0-9]\.[0-9](?:[0-9])?$)/;

const Dialog = {
    name: "SetMoneyDialog",
    components: {ImageUploadMultiple},
    props: {
        type: {
            type: Number,
            required: true
        },
        shopInfo: {
            type: Object,
            required: true
        },
        shopNo: {
            type: String,
            required: true
        },
    },
    data() {
        const checkMoney = (rule, value, callback) => {
            if(this.type === 1) {
                callback();
            } else if(Number(value) > this.shopInfo.vas_money) {
                callback(new Error('扣款金额不能大于当前增值服务费'));
            } else {
                callback();
            }
        };

        return {
            dialogVisible: false,
            loading: false,
            ruleForm: {
                money: '',
                remark: '',
                reason: '',
                certificate_images: []
            },
            rules: {
                money: [
                    {required: true, message: '请填写金额', trigger: 'blur'},
                    {pattern: numberReg, message: '金额必须为数字', trigger: 'blur'},
                    {validator: checkMoney, trigger: 'blur'},
                ],
                remark: [
                    {required: true, message: '请填写充值备注', trigger: 'blur'}
                ],
                reason: [
                    {required: true, message: '请填写扣除原因', trigger: 'blur'}
                ],
                certificate_images: [
                    {required: true, message: '请上传扣除凭证', trigger: 'change'}
                ],
            }
        };
    },
    computed: {},
    filters: {},
    mounted() {

    },
    methods: {

        /**
         * 显示弹窗
         */
        show() {
            this.dialogVisible = true;
        },

        /**
         * 显示弹窗
         */
        close() {
            this.dialogVisible = false;
        },

        /**
         * 关闭弹窗前
         */
        beforeClose() {
            this.ruleForm = {
                money: '',
                remark: '',
                reason: '',
                certificate_images: []
            };
            this.loading = false;
            this.$emit('close');
        },

        /**
         * 确定选择
         */
        handleSubmit() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.loading = true;
                    let url = this.type === 1 ? '/student/shop_vas_money%5Crecharge' : '/student/shop_vas_money%5Cdeduct';
                    let params = deepClone(this.ruleForm);

                    params.certificate_images = params.certificate_images.join();
                    params.shop_no = this.shopNo;
                    this.$post(url, params, resp => {
                        if (resp.code === 1) {
                            this.$notify({
                                title: '提示',
                                message: '操作成功',
                                duration: 2000,
                                type: 'success'
                            });
                            this.$emit('confirm', true);
                        } else {
                            this.$notify({
                                title: '提示',
                                message: resp.msg,
                                duration: 2000,
                                type: 'warning'
                            });
                        }
                        this.loading = false;
                    });
                }
            });
        }
    }
};

export default Dialog;

const setMoneyDialog = function (config) {
    return new Promise((resolve) => {
        const DialogConstructor = Vue.extend(Dialog);
        const instance = new DialogConstructor({
            propsData: config,
        }).$mount();

        instance.$on("close", function () {
            setTimeout(() => {
                document.body.removeChild(instance.$el);
                instance.$destroy();
            }, 300);
            resolve(false);
        });
        instance.$on("confirm", function (data) {
            instance.close();
            resolve(data);
        });
        document.body.appendChild(instance.$el);
        instance.dialogVisible = true;
    });
};

export { setMoneyDialog };

</script>

<style scoped lang="less">
.wrapper {
    .dialog-content {
        display: flex;
        flex-direction: column;
        .form-title {
            font-size: 14px;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #333333;
            margin-bottom: 24px;
        }
        .form-tips {
            font-size: 12px;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #C2C2C2;
        }
        .shop-info {
            display: flex;
            flex-wrap: wrap;
            justify-content: space-between;
            &-item {
                display: flex;
                align-items: center;
                font-size: 14px;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                margin-bottom: 20px;
                width: 50%;
                &__label {
                    color: #555555;
                }
                &__value {
                    color: #999999;
                }
            }
        }
    }
    .dialog-footer {
        display: flex;
        justify-content: center;
        margin-top: 12px;
    }

    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-dialog__body {
        padding: 24px 60px 32px;
    }
    /deep/ .el-dialog {
        margin-top: 10vh!important;
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>
